// 定义全局状态
const store = {
  state: {
    userInfo: null,
    token: '',
    studentId: '',
    coachId: '',
    mac: '',
    baseData: null,
    gradeList: null
  },
  listeners: {},

  // 初始化：从本地缓存加载状态
  init() {
    const token = wx.getStorageSync('token');
    const studentId = wx.getStorageSync('studentId');
    const coachId = wx.getStorageSync('coachId');
    const mac = wx.getStorageSync('mac');
    const userInfo = wx.getStorageSync('userInfo');
    const baseData = wx.getStorageSync('baseData');
    const gradeList = wx.getStorageSync('gradeList')
    // 初始化状态（避免直接修改state，用set方法触发监听）
    if (token) this.setToken(token);
    if (studentId) this.setStudentId(studentId);
    if (coachId) this.setCoachId(coachId);
    if (mac) this.setMac(mac);
    if (userInfo) this.setUserInfo(userInfo);
    if (baseData) this.setbaseData(baseData);
    if (gradeList) this.setgradeList(gradeList);
  },

  // 修改状态的方法（严格控制状态修改入口）
  setUserInfo(info) {
    this.state.userInfo = info;
    wx.setStorageSync('userInfo', info); // 持久化
    this.emit('userInfoChange', info);
  },
  setCoachId(id) {
    this.state.coachId = id;
    wx.setStorageSync('coachId', id); // 持久化
    this.emit('coachIdChange', id);
  },
  setMac(mac) {
    this.state.mac = mac;
    wx.setStorageSync('mac', mac); // 持久化
    this.emit('macChange', mac);
  },
  setStudentId(id) {
    this.state.studentId = id;
    wx.setStorageSync('studentId', id); // 持久化
    this.emit('studentIdChange', id);
  },
  setToken(token) {
    this.state.token = token;
    wx.setStorageSync('token', token); // 持久化
    this.emit('tokenChange', token);
  },
  setbaseData(baseData) {
    this.state.baseData = baseData;
    wx.setStorageSync('baseData', baseData); // 持久化
    this.emit('baseDataChange', baseData);
  },
  setgradeList(gradeList) {
    this.state.gradeList = gradeList;
    wx.setStorageSync('gradeList', gradeList); // 持久化
    this.emit('gradeListChange', gradeList);
  },
  // 新增：清除所有状态（如退出登录）
  clearAll() {
    this.state = {
      userInfo: null,
      token: '',
      studentId: '',
      coachId: '',
      mac: '',
      baseData: null,
      gradeList: null
    };
    wx.clearStorageSync(); // 清空本地缓存
    // 通知所有状态变更
    this.emit('userInfoChange', null);
    this.emit('tokenChange', '');
    this.emit('studentIdChange', '');
    this.emit('coachIdChange', '');
    this.emit('macChange', '');
    this.emit('baseDataChange', '');
    this.emit('gradeListChange', '');
  },

  // 事件监听：新增off方法用于取消监听
  on(event, callback) {
    if (!this.listeners[event]) {
      this.listeners[event] = [];
    }
    // 存储回调函数，便于后续移除
    this.listeners[event].push(callback);
  },
  off(event, callback) {
    if (!this.listeners[event]) return;
    // 移除指定回调（若不传callback，则移除该事件的所有监听）
    this.listeners[event] = callback 
      ? this.listeners[event].filter(cb => cb !== callback)
      : [];
  },
  emit(event, data) {
    if (this.listeners[event]) {
      // 复制一份监听数组，避免执行中移除监听导致的异常
      [...this.listeners[event]].forEach(callback => callback(data));
    }
  }
};

// 初始化：小程序启动时加载本地缓存的状态
store.init();

module.exports = store;